import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'barraProductM',
  state: {},
  effects: {
    // 获取业绩基准下拉数据
    *getPerformanceBenchmarks({ payload, callback }, { call, put, select }) {
      const res = yield call(api.workStation.getPerformanceBenchmarks, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res?.msg || '获取业绩基准失败！');
      }
    },

    // 新增模拟组合
    *addPortfolio({ payload, callback }, { call, put }) {
      const response = yield call(api.workStation.addPortfolio, payload);
      // const response = { code: 0 }
      if (response && response.code == 0) {
        message.success('新增成功');
        callback(true);
      } else {
        if (response.code == 2) {
          message.error('组合名称不可重复');
        } else {
          message.error(response?.msg || '新增模拟组合失败！');
        }
      }
    },

    // 编辑模拟组合
    *editPortfolio({ payload, callback }, { call, put }) {
      const response = yield call(api.workStation.editPortfolio, payload);
      // const response = { code: 0 }

      if (response && response.code == 0) {
        message.success('编辑成功');
        callback(true);
      } else {
        if (response.code == 2) {
          message.error('组合名称不可重复');
        } else {
          message.error(response?.msg || '编辑模拟组合失败！');
        }
      }
    },

    // 删除模拟组合
    *deletePortfolio({ payload, callback }, { call, put }) {
      const response = yield call(api.workStation.deletePortfolio, payload);
      // const response = { code: 0 }

      if (response && response.code == 0) {
        message.success('删除成功');
        callback(true);
      } else {
        message.error(response?.msg || '删除失败！');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
